public class ConversationFragment extends Fragment implements ConversationView{
	
	
    private View view;
    private List<Conversation> conversationList = new LinkedList<>();
    private ConversationAdapter adapter;
    private ListView listView;
    private ConversationPresenter presenter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null){
            view = inflater.inflate(R.layout.fragment_conversation, container, false);

            listView = (ListView) view.findViewById(R.id.list);
            adapter = new ConversationAdapter(getActivity(), R.layout.item_conversation, conversationList);
            listView.setAdapter(adapter);

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					//会话列表点击跳转
                    conversationList.get(position).startChat(getActivity());
                }
            });
            presenter = new ConversationPresenter(this);
            presenter.getConversation();
            registerForContextMenu(listView);
        }
        return view;

    }

    //Resume事件
    @Override
    public void onResume(){
        super.onResume();
        refresh();
    }

    //接口_载入会话
    @Override
    public void initCons(List<TIMConversation> conversationList) {
        this.conversationList.clear();
		this.conversationList=conversationList;
		refresh();
    }

    //接口_更新会话
    @Override
    public void updateCons(TIMMessage message) {
        if (message == null){
            adapter.notifyDataSetChanged();
            return;
        }
        Conversation conversation = new Conversation(message.getConversation());
        conversation.setLastMessage(MessageFactory.getMessage(message));
        conversationList.add(conversation);
        refresh();
    }

    //接口_删除会话
    @Override
    public void removeCons(String identify) {
        Iterator<Conversation> iterator = conversationList.iterator();
        while(iterator.hasNext()){
            Conversation conversation = iterator.next();
            if (conversation.getIdentify()!=null&&conversation.getIdentify().equals(identify)){
                iterator.remove();
                adapter.notifyDataSetChanged();
                return;
            }
        }
    }

    //接口_刷新,同时刷新父activity未读数
    @Override
    public void refresh() {
        Collections.sort(conversationList);
        adapter.notifyDataSetChanged();
		
        if(getTotalUnreadNum() == 0){
            ((HomeActivity) getActivity()).setMsgUnread(false,"0");
        }else {
            ((HomeActivity) getActivity()).setMsgUnread(true,getUnreadNum()+"");
        }
    }

    //创建窗口
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        Conversation conversation = conversationList.get(info.position);
        if (conversation instanceof Conversation){
            menu.add(0, 1, Menu.NONE, "删除会话");
			//有空了再添加更多功能，比如真微信的多条标记，转发…等
        }
    }

	//窗口点击事件
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        Conversation conversation = (Conversation) conversationList.get(info.position);
        switch (item.getItemId()) {
            case 1:
                if (conversation != null){
                  presenter.delConversation(conversation.getType(), conversation.getIdentify())
                  conversationList.remove(conversation);
                  adapter.notifyDataSetChanged();
                }
                break;
            default:
                break;
        }
        return super.onContextItemSelected(item);
    }

    //获取未读消息数目
    private long getUnreadNum(){
        long num = 0;
        for (Conversation conversation : conversationList){
            num += conversation.getUnreadNum();
        }
        return num;
    }


}
